/*--------------------------------------------------------------------------
Oaxaca-Blinder decomposition
Last modification: 04/30/2021
Uses:	           merged_data.dta, perceptions_data.dta
Produces:          Oaxaca-Blinder
--------------------------------------------------------------------------*/

global raw  "C:\Users\USERNAME\"
global code "${root}\code"
global data "${root}\data"
global res  "${root}\results"

* Oaxaca-Blinder
#delimit;
use "${data}\perceptions_data.dta", replace;
merge m:1 pais ano using "${data}\merged_data.dta", nogen;
merge m:1 pais ano using "${data}\gini.dta";

tempname temporal; tempfile temporal_file; postfile `temporal' str40(period bar value comp) using `temporal_file', replace;

           levelsof ano , local(years) ; foreach yr of local years  {;          gen yr`yr' = (ano == `yr')  ; }; drop yr1997;
loc i = 0; levelsof pais, local(paises); foreach p  of local paises {; loc ++i; gen ct`i'  = (pais == "`p'"); }; drop ct1;

tab maxedu, gen(educ);

gen     assets = 1 if cloaca == 1 & computadora  == 1 & lavarropas == 1 & telefono_fijo == 1 & auto == 1;
replace assets = 0 if cloaca == 0 | computadora  == 0 | lavarropas == 0 | telefono_fijo == 0 | auto == 0;

gen miss_assets   = (missing(cloaca) | missing(computadora) | missing(lavarropas) | missing(telefono_fijo) | missing(auto)); qui sum assets, d; replace assets = r(mean) if miss_assets == 1;
gen miss_catolico = (missing(catolico)); qui sum catolico, d; replace catolico = r(mean) if miss_catolico == 1;
gen miss_ideology = (missing(ideology)); qui sum ideology, d; replace ideology = r(mean) if miss_ideology == 1;

glo yr1 = 2002;
glo yr2 = 2013;

oaxaca unfair_all gini_lel edad edad2 hombre casado educ3 educ4 pea desocupa assets miss_asse ideology miss_ideo catolico miss_cato [iw=pondera] if inlist(ano,${yr1},${yr2}), by(ano) relax;

matrix yr${yr1}${yr2} = r(table);

glo unfair_ini   =  yr${yr1}${yr2}[1,1]*100;
glo unfair_fin   =  yr${yr1}${yr2}[1,2]*100;
glo change       =  yr${yr1}${yr2}[1,3]*100;
glo unexplained  = (yr${yr1}${yr2}[1,5] + yr${yr1}${yr2}[1,6]) * 100;
glo explained    =  yr${yr1}${yr2}[1,4]*100;
glo gini         =  yr${yr1}${yr2}[1,7]*100;
glo demographics =  $explained - $gini;

glo dunfair_ini   : display %16.2fc $unfair_ini;
glo dunfair_fin   : display %16.2fc $unfair_fin;
glo dunexplained  : display %16.2fc $unexplained;
glo dexplained    : display %16.2fc $explained;
glo dgini         : display %16.2fc $gini;
glo ddemographics : display %16.2fc $demographics;


post `temporal' ("2002-2013") ("1") ("$unfair_ini") ("ini");
post `temporal' ("2002-2013") ("2") ("$unfair_fin") ("fin");
post `temporal' ("2002-2013") ("2") ("$change") ("z1");
post `temporal' ("2002-2013") ("3") ("$unfair_fin") ("a1");
post `temporal' ("2002-2013") ("3") ("$explained") ("explained");
post `temporal' ("2002-2013") ("3") ("$unexplained") ("unexplained");
post `temporal' ("2002-2013") ("4") ("$unfair_ini") ("a1");

postclose `temporal'; use `temporal_file', clear; destring, replace; compress;


replace value = value - 50 if value > 50;

#delimit;

graph bar (sum) value, over(comp) over(bar, relabel(1 "2002" 2 "2013" 3 "Change 2002-2013" 4  " "))  asyvars stack
					   bar(1, color(none)   lcolor(none))
					   bar(2, color(green)  lcolor(black))
					   bar(3, color(maroon) lcolor(black))
					   bar(4, color(navy)   lcolor(black))
					   bar(5, color(orange) lcolor(black))
					   bar(6, color(none)   lcolor(gs12) lpattern(dash))
			    	/* blabel(bar, position(inside) color(white) format(%4.1f)) */
					   ylabel(0 "50" 10 "60" 20 "70" 30 "80" 40 "90", angle(0) nogrid)
					   ytitle("Percent unfair or very unfair")
					   text(34 7  "$dunfair_ini"                           , color(white) size(small))
					   text(20 33 "$dunfair_fin"                             , color(white) size(small))
					   text(36 59 "$dunexplained"                             , color(white) size(small))
					   text(30 59 "$dexplained"                             , color(white) size(small))
					   text(31 37 "Change in" "fairness" "views" "2002-2013", color(black) size(small))
					   text(34 62.7 "Unexplained"                           , color(white) size(small))
					   text(28 62.6 "Explained"                             , color(white) size(small))
					   text(30 84 "$dgini Gini coefficient"                   , color(black) size(small))
					   text(26 86 "$ddemographics Composition effect"                 , color(black) size(small))
					   text(31 50 "`=ustrunescape("\u23A7")'" "`=ustrunescape("\u23A8")'" "`=ustrunescape("\u23A9")'", size(10) width(vsmall) color(black) )
					   text(29 75 "`=ustrunescape("\u23A7")'" "`=ustrunescape("\u23A8")'" "`=ustrunescape("\u23A9")'", size(7)  width(vsmall) color(black) ) 
					   legend(off) graphregion(color(white)) bgcolor(white);
					   graph export "${res}\fig-oaxaca-0213.pdf", replace;
					   graph export "${res}\fig-oaxaca-0213.png", as(png) width(3000) replace;


/* End of do-file */
exit
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> 